package controller

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"yak.admin/src/entity"
	"yak.admin/src/middleware"
	"yak.admin/src/module/backend/hnyj/form"
	"yak.admin/src/module/backend/hnyj/repository"
	"yak.admin/src/util"
)

func Ready(c *gin.Context) {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	var page middleware.Page
	if admin.Type == util.TypeDrug {
		page.Title = "上报列表"
		page.Linker = "report-ready"
		page.Header = "药品上报列表"
		page.Tpl = "hnyj/drug/ready/index.tpl"
	}
	if admin.Type == util.TypeDevice {
		page.Title = "上报列表"
		page.Linker = "report-ready"
		page.Header = "医疗器械上报列表"
		page.Tpl = "hnyj/device/ready/index.tpl"
	}
	c.Set("Page", page)
}

func ReadyList(c *gin.Context)  {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	if admin.Type == util.TypeDrug {
		drugSearch := repository.DrugReportRepository{Status: util.PendingStatus, Province: admin.Province, City: admin.City, County: admin.County, IsSuper: admin.IsSuper()}
		data, count := drugSearch.FindReports(c)
		middleware.Datalist(c, data, count)
	}
	if admin.Type == util.TypeDevice {
		drugSearch := repository.DeviceReportRepository{Status: util.PendingStatus, Province: admin.Province, City: admin.City, County: admin.County, IsSuper: admin.IsSuper()}
		data, count := drugSearch.FindReports(c)
		middleware.Datalist(c, data, count)
	}
}

func Watch(c *gin.Context)  {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	f := form.ReportCheckForm{}
	if err := util.DefaultGetValidParams(c, &f); err != nil {
		middleware.ResultFail(c, err.Error(), nil)
		return
	}
	f.IsSuper = admin.IsSuper()
	f.Province = admin.Province
	f.City = admin.City
	f.County = admin.County
	f.Status = util.FollowStatus
	f.AdminId = admin.Id
	if admin.Type == util.TypeDrug {
		if f.Drug() {
			middleware.ResultSuccess(c, "", nil)
			return
		}
	}
	if admin.Type == util.TypeDevice {
		if f.Device() {
			middleware.ResultSuccess(c, "", nil)
			return
		}
	}

	middleware.ResultFail(c, "关注失败", nil)
}

func Unwatch(c *gin.Context)  {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	f := form.ReportCheckForm{}
	if err := util.DefaultGetValidParams(c, &f); err != nil {
		middleware.ResultFail(c, err.Error(), nil)
		return
	}
	f.IsSuper = admin.IsSuper()
	f.Province = admin.Province
	f.City = admin.City
	f.County = admin.County
	f.Status = util.IgnoreStatus
	f.AdminId = admin.Id
	if admin.Type == util.TypeDrug {
		if f.Drug() {
			middleware.ResultSuccess(c, "", nil)
			return
		}
	}
	if admin.Type == util.TypeDevice {
		if f.Device() {
			middleware.ResultSuccess(c, "", nil)
			return
		}
	}

	middleware.ResultFail(c, "操作失败", nil)
}

func ReadyView(c *gin.Context)  {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	var page middleware.Page
	if admin.Type == util.TypeDrug {
		var drugSearch repository.DrugReportRepository
		id, _ := strconv.Atoi(c.Query("id"))
		page.Data = map[string]interface{} {
			"report": drugSearch.FindReportView(uint(id)),
		}
		page.Tpl = "hnyj/drug/ready/view.tpl"
	}
	if admin.Type == util.TypeDevice {
		var deviceSearch repository.DeviceReportRepository
		id, _ := strconv.Atoi(c.Query("id"))
		page.Data = map[string]interface{} {
			"report": deviceSearch.FindReportView(uint(id)),
		}
		page.Tpl = "hnyj/device/ready/view.tpl"
	}
	c.Set("Page", page)
}

func History(c *gin.Context)  {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	var page middleware.Page
	if admin.Type == util.TypeDrug {
		repos := repository.DrugReportRepository{}
		page.Tpl = "hnyj/drug/history/index.tpl"
		page.Title = "上报查询"
		page.Linker = "report-history"
		page.Header = "历史药品上报列表"
		page.Data = map[string]interface{}{
			"CategoryLabels": repos.DrugCategoryLabels(),
			"RiskLabels": repos.DrugRiskLabels(),
			"StatusLabels": repos.StatusLabels(),
		}
	}

	if admin.Type == util.TypeDevice {
		repos := repository.DeviceReportRepository{}
		page.Tpl = "hnyj/device/history/index.tpl"
		page.Title = "上报查询"
		page.Linker = "report-history"
		page.Header = "历史医疗器械上报列表"
		page.Data = map[string]interface{}{
			"RiskAnalysisLabels": repos.DeviceAnalysisLabels(),
			"RiskDescLabels": repos.DeviceDescLabels(),
			"StatusLabels": repos.StatusLabels(),
		}
	}

	c.Set("Page", page)
}

func HistoryList(c *gin.Context)  {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	if admin.Type == util.TypeDrug {
		drugSearch := repository.DrugReportRepository{IsSuper: admin.IsSuper()}
		data, count := drugSearch.FindHistoryReports(c, admin)
		middleware.Datalist(c, data, count)
	}
	if admin.Type == util.TypeDevice {
		deviceSearch := repository.DeviceReportRepository{IsSuper: admin.IsSuper()}
		data, count := deviceSearch.FindHistoryReports(c, admin)
		middleware.Datalist(c, data, count)
	}
}

func HistoryExcel(c *gin.Context)  {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	if admin.Type == util.TypeDrug {
		drugSearch := repository.DrugReportRepository{IsSuper: admin.IsSuper()}
		header, data := drugSearch.FindHistoryExcel(c, admin)
		middleware.DataExcel(c, data, header)
	}
	if admin.Type == util.TypeDevice {
		deviceSearch := repository.DeviceReportRepository{IsSuper: admin.IsSuper()}
		header, data := deviceSearch.FindHistoryExcel(c, admin)
		middleware.DataExcel(c, data, header)
	}
}

func HistoryView(c *gin.Context)  {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	var page middleware.Page
	if admin.Type == util.TypeDrug {
		var drugSearch repository.DrugReportRepository
		id, _ := strconv.Atoi(c.Query("id"))
		page.Tpl = "hnyj/drug/history/view.tpl"
		page.Data = map[string]interface{}{
			"report": drugSearch.FindReport(uint(id)),
		}
	}
	if admin.Type == util.TypeDevice {
		var deviceSearch repository.DeviceReportRepository
		id, _ := strconv.Atoi(c.Query("id"))
		page.Tpl = "hnyj/device/history/view.tpl"
		page.Data = map[string]interface{}{
			"report": deviceSearch.FindReportView(uint(id)),
		}
	}
	c.Set("Page", page)
}

func Summary(c *gin.Context)  {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	var page middleware.Page
	if admin.Type == util.TypeDrug {
		repos := repository.DrugReportRepository{}
		page.Tpl = "hnyj/drug/summary/index.tpl"
		page.Title = "风险汇总"
		page.Linker = "report-summary"
		page.Header = "已关注风险表"
		page.Data = map[string]interface{}{
			"CategoryLabels": repos.DrugCategoryLabels(),
			"RiskLabels": repos.DrugRiskLabels(),
			"StatusLabels": repos.StatusLabels(),
		}
	}
	if admin.Type == util.TypeDevice {
		repos := repository.DeviceReportRepository{}
		page.Tpl = "hnyj/device/summary/index.tpl"
		page.Title = "风险汇总"
		page.Linker = "report-summary"
		page.Header = "已关注风险表"
		page.Data = map[string]interface{}{
			"RiskAnalysisLabels": repos.DeviceAnalysisLabels(),
			"RiskDescLabels": repos.DeviceDescLabels(),
			"StatusLabels": repos.StatusLabels(),
		}
	}
	c.Set("Page", page)
}

func SummaryList(c *gin.Context)  {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	if admin.Type == util.TypeDrug {
		var drugSearch repository.DrugReportRepository
		data, count := drugSearch.FindSummaryReports(c, admin)
		middleware.Datalist(c, data, count)
	}
	if admin.Type == util.TypeDevice {
		var deviceSearch repository.DeviceReportRepository
		data, count := deviceSearch.FindSummaryReports(c, admin)
		middleware.Datalist(c, data, count)
	}
}

func SummaryExcel(c *gin.Context)  {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	if admin.Type == util.TypeDrug {
		drugSearch := repository.DrugReportRepository{IsSuper: admin.IsSuper()}
		header, data := drugSearch.FindSummaryExcel(c, admin)
		middleware.DataExcel(c, data, header)
	}
	if admin.Type == util.TypeDevice {
		deviceSearch := repository.DeviceReportRepository{IsSuper: admin.IsSuper()}
		header, data := deviceSearch.FindSummaryExcel(c, admin)
		middleware.DataExcel(c, data, header)
	}
}

func SummaryView(c *gin.Context)  {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	var page middleware.Page
	if admin.Type == util.TypeDrug {
		var drugSearch repository.DrugReportRepository
		id, _ := strconv.Atoi(c.Query("id"))
		page.Tpl = "hnyj/drug/summary/view.tpl"
		page.Data = map[string]interface{}{
			"report": drugSearch.FindReport(uint(id)),
		}
	}
	if admin.Type == util.TypeDevice {
		var deviceSearch repository.DeviceReportRepository
		id, _ := strconv.Atoi(c.Query("id"))
		page.Tpl = "hnyj/device/summary/view.tpl"
		page.Data = map[string]interface{}{
			"report": deviceSearch.FindReportView(uint(id)),
		}
	}
	c.Set("Page", page)
}
